package com.hua.demo.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hua.JwtUtils;
import com.hua.MD5;
import com.hua.demo.edu.entity.UcenterMember;
import com.hua.demo.edu.entity.VO.RegisterVo;
import com.hua.demo.edu.mapper.UcenterMemberMapper;
import com.hua.demo.edu.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hua.exceptionhandler.GuliException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author huazi
 * @since 2021-12-02
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Override
    public String login(UcenterMember ucenterMember) {
        String mobile = ucenterMember.getMobile();
        String password = ucenterMember.getPassword();
        if(StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
            throw new GuliException(20001,"密码或手机号为空");
        }
        QueryWrapper<UcenterMember> qu = new QueryWrapper<>();
        qu.eq("mobile",mobile);
        UcenterMember one = this.getOne(qu);
        if(one==null){
            throw new GuliException(20001,"登录失败");
        }if(!MD5.encrypt(password).equals(one.getPassword())){
            throw new GuliException(20001,"登陆失败");
        }
        if(one.getIsDisabled()){
            throw new GuliException(20001,"登陆失败");
        }
        String jwtToken = JwtUtils.getJwtToken(one.getId(), one.getNickname());
        return jwtToken;
    }

    @Override
    public void register(RegisterVo registerVo) {
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String nickname = registerVo.getNickname();
        String code = registerVo.getCode();
        if(StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)||

                StringUtils.isEmpty(nickname)||StringUtils.isEmpty(code)){
            throw new GuliException(20001,"手机号或者密码为空");
        }
        //判断验证码
        //获取redis验证码
        QueryWrapper<UcenterMember> qu = new QueryWrapper<>();
        qu.eq("mobile",mobile);
        UcenterMember one = this.getOne(qu);
        if(!(one==null)){
            throw new GuliException(20001,"手机号已被注册");
        }
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMobile(mobile);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setNickname(registerVo.getNickname());
        ucenterMember.setIsDisabled(false);//用户不禁用
        ucenterMember.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        boolean save = this.save(ucenterMember);
        System.out.println("注册成功");
    }
    //根据openid判断
    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }
}
